package com.xiaoshu.controller;

import com.xiaoshu.model.Video;
import com.xiaoshu.service.VideoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * @author 何永豪
 * @className RouteController
 * @description TODO
 * @date 2023/11/10 18:12
 */
@Controller
public class RouteController {

    @Resource
    private VideoService videoService;

    @Value("${video.number}")
    private Integer viewNumber;

    @GetMapping("/index")
    public String hello(Model model){
        List<Video> videos = videoService.allVideos();
        List<Video> res = new LinkedList<>();
        for (int i = 0; i < viewNumber; i++) {
            int index = new Random().nextInt(videos.size());
            res.add(videos.get(index));
        }
        model.addAttribute("videos",res);
        return "index";
    }


}
